/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.command;

import cef.command.Command;
import cef.command.CommandException;
import cef.command.listener.exe.CommandExeEvent;

import coalescent.br.*;
import coalescent.cef.event.RunBackwardRecursionEvent;

import commons.util.MathUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Calculates population genetic recursion along with a number of computations based on it.
 *
 * @author Susanta Tewari
 * @version 2.0.0
 * @history Created on 04/21/2012
 * @since 1.3.0
 */
@SuppressWarnings("TypeMayBeWeakened")
public class RunBackwardRecursionCommand extends Command<RunBackwardRecursionEvent> {

    /** Field description */
    private List<Integer> currentRecursionCacheSize = Collections.EMPTY_LIST;

    /** Field description */
    private List<BRWComp<?, ?>> br_walker_comps;

    /**
     * Constructs ...
     *
     *
     * @param event
     */
    public RunBackwardRecursionCommand(RunBackwardRecursionEvent event) {

        super(event);

        br_walker_comps = new ArrayList<>(10);
    }

    @Override
    public void executeImpl() throws CommandException, InterruptedException {


        // 2-2. get the statistic
        BRSPopGen<?, ?, ?, ?, ?, ?, ?> statistic = getEvent().getBackwardRecursion();


        // 3. prepare for the job
        // 3-1. Create an instance of the recursion
        BRWalker walker = new BRWalker(statistic);

        walker.addBRWalkerExeListener(new BRWalkerExeListener() {

            @Override
            public void receivedExactRecursionExeEvent(BRWalkerExeEvent event) {
                RunBackwardRecursionCommand.this.receivedExactRecursionExeEvent(event);
            }

        });


        // 3-2. attach the computations
        for (BRWComp listener : getEvent().getBRWalkerComputations()) {
            walker.addComputation(listener);
        }


        // 4. do the job. run the recursion
        walker.runRecursion();
        setCmdFinished(true);
        fireOutputEvent(getCommandOutputEvent());
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public static boolean isCancellable() {
        return true;
    }

    /**
     * Returns the exact recursion outputListeners that were attached to this command. These outputListeners compute
     * various aspects and their {@code toString} method provides a textual summary of the results.
     *
     * @return recursion computations
     */
    public List<BRWComp<?, ?>> getBRWalkerComputations() {

        checkCmdFinished();

        return br_walker_comps;
    }

    /**
     * Method description
     *
     *
     * @param event
     */
    private void receivedExactRecursionExeEvent(BRWalkerExeEvent event) {


        // save the current cacheSize
        this.currentRecursionCacheSize = event.getCacheSize();


        // prepare the update string
        long totalCacheSize = MathUtil.sumDoublePrecision(currentRecursionCacheSize).longValue();
        String updateString = "Total Cache Size: " + totalCacheSize;


        // prepare the event
        CommandExeEvent cmdExeEvent = new CommandExeEvent(this, updateString);


        // fire the exe event
        fireExeEvent(cmdExeEvent);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public List<Integer> getCurrentRecursionCacheSize() {
        return currentRecursionCacheSize;
    }
}
